package com.gxar.uaa.server.base.dubbo;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxar.uaa.api.client.IMenuClient;
import com.gxar.uaa.api.dto.PageResult;
import com.gxar.uaa.api.dto.menu.SysMenu;
import com.gxar.uaa.api.dto.user.SysUserId;
import com.gxar.uaa.server.base.common.constant.CommonConstant;
import com.gxar.uaa.server.db.service.MenuService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.apache.dubbo.config.annotation.DubboService;

import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author linsy
 * @version 1.0
 * @created 2024/1/23 10:14
 */
@Slf4j
@DubboService(version = "1.0.0")
@RequiredArgsConstructor
public class MenuClientImpl implements IMenuClient {

    private final MenuService menuService;

    @Override
    public List<SysMenu> findMenuByRoles(String app, String roleCodes) {
        List<SysMenu> result = null;
        if (StringUtils.isNotEmpty(roleCodes)) {
            Set<String> roleSet = Arrays.stream(roleCodes.split(",")).collect(Collectors.toSet());
            result = menuService.findByRoleCodes(app, roleSet, CommonConstant.PERMISSION);
        }
        return result;
    }

    @Override
    public PageResult<SysUserId> findUserByMenuUrl(Integer pageNum, Integer pageSize, String url, String urlMethod) {
        Page<Object> page = PageHelper.startPage(pageNum, pageSize);
        List<SysUserId> responseList = menuService.findUserByMenuUrl(url, urlMethod);
        if (CollectionUtils.isEmpty(responseList)) {
            return new PageResult<>();
        }
        PageInfo<SysUserId> pageInfo = new PageInfo<>(responseList);
        pageInfo.setPageNum(pageNum);
        pageInfo.setTotal(page.getTotal());
        return new PageResult<>(pageInfo.getSize(), pageInfo.getPageNum(), pageInfo.getTotal(), responseList);
    }

}
